﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll.IObjectBlls;
using CSP.Bll;
using CSP.Common.Web;
using System.Collections.ObjectModel;
using CSP.Web.UserControls;
using CSP.Web.Master;

namespace CSP.Web.MyHome.Address
{
    public partial class List : CSPMyHome
    {

        private CSPBllFactory _factory;

        private IAddressBll _addressBll;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((HomeMaster)Master).SelectedNavigationLink = MyHomeLeftNavigationLink.Address;
            if (!IsPostBack)
            {
                if (Request.QueryString["deleteid"] != null)
                {
                    //获取Post对象流 并转化为json字符串
                    //MemoryStream stream = .
                    //using (StreamReader stream = new StreamReader(Request.InputStream))
                    //{
                    //    jsonData = stream.ReadToEnd();
                    //}
                    Response.ClearContent();
                    Response.ContentType = "text/plain";
                    string result = RemoveAddressBll(Convert.ToInt32(Request.QueryString["deleteid"]));
                    Response.Write(result);
                    this.Response.End();
                }
                else
                {
                    BindAddressUI();
                }

            }
        }

        /// <summary>
        /// 绑定地址
        /// </summary>
        private void BindAddressUI()
        {
            ICollection<Model.CSP.Address> addresses = GetAddressesByUser();
            if (addresses == null)
            {
                addresses = new Collection<Model.CSP.Address>();
            }
            this.R_Address.DataSource = addresses;
            this.R_Address.DataBind();
        }

        private ICollection<Model.CSP.Address> GetAddressesByUser()
        {
            ICollection<Model.CSP.Address> result = null;
            _factory = CSPBllFactory.GetInstance();
            using (_addressBll = _factory.GetAddressBll())
            {
                result = _addressBll.FindByUser(UserProvider.UserName);
            }

            return result;
        }

        /// <summary>
        /// 删除地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private string RemoveAddressBll(int id)
        {
            _factory = CSPBllFactory.GetInstance();
            _addressBll = _factory.GetAddressBll();

            try
            {
                var address = _addressBll.DeleteByIdAndUser(id, UserProvider.UserName);
                if (address == null)
                {
                    return "{\"status\":2,\"msg\":\"权限不足!\"}";
                }

                return "{\"status\":1,\"msg\":\"操作成功!\"}";
            }
            catch (Exception ex)
            {
                LoggerProvider.Error(id, ex);
                return "{\"status\":0,\"msg\":\"操作失败!\"}";
            }
        }

    }
}